/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hadss.avoid_area

import androidx.annotation.NonNull
import androidx.window.java.layout.WindowInfoTrackerCallbackAdapter
import androidx.window.layout.WindowInfoTracker
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel

/**
 * 获取折叠屏状态
 */
private const val FOLDER_STATE_CHANNEL_NAME = "avoid_area/folder_state"

/**
 *  区域避让method方法
 */
private const val FLUTTER_AVOID_AREA_METHOD = "flutter_avoid_area_method"

/**
 *  区域避让监听方法
 */
private const val FLUTTER_AVOID_AREA_EVENT = "flutter_avoid_area_event"

/** AvoidAreaPlugin */
class AvoidAreaPlugin : FlutterPlugin, ActivityAware {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var methodChannel: MethodChannel
    private lateinit var eventChannel: EventChannel
    private lateinit var folderStateChannel: EventChannel
    var windowInfoTracker: WindowInfoTrackerCallbackAdapter? = null
    var folderStateStreamHandler: FolderStateStreamHandler? = null

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        folderStateChannel =
            EventChannel(flutterPluginBinding.binaryMessenger, FOLDER_STATE_CHANNEL_NAME)
        methodChannel =
            MethodChannel(flutterPluginBinding.binaryMessenger, FLUTTER_AVOID_AREA_METHOD)
        eventChannel =
            EventChannel(flutterPluginBinding.binaryMessenger, FLUTTER_AVOID_AREA_EVENT)
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        folderStateChannel.setStreamHandler(null)
        methodChannel.setMethodCallHandler(null)
        eventChannel.setStreamHandler(null)
    }

    override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) {
        windowInfoTracker =
            WindowInfoTrackerCallbackAdapter(
                WindowInfoTracker.Companion.getOrCreate(
                    activityPluginBinding.activity
                )
            )
        folderStateStreamHandler = FolderStateStreamHandler(windowInfoTracker!!)
        folderStateChannel.setStreamHandler(folderStateStreamHandler)
        windowInfoTracker?.addWindowLayoutInfoListener(
            activityPluginBinding.activity,
            Runnable::run,
            folderStateStreamHandler!!
        )
        methodChannel.setMethodCallHandler(AvoidAreaMethodCallHandler(activityPluginBinding.activity))
    }

    override fun onDetachedFromActivityForConfigChanges() {
    }

    override fun onReattachedToActivityForConfigChanges(p0: ActivityPluginBinding) {
    }

    override fun onDetachedFromActivity() {
        windowInfoTracker?.removeWindowLayoutInfoListener(folderStateStreamHandler!!)
    }
}